package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;

import com.quasar.hpatient.R;

import java.util.ArrayList;
import java.util.List;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;

public class ListDialog extends BaseDialog {

    private ArrayList<ListModel> mDatas = new ArrayList();

    public ListDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_list;
    }

    @Override
    protected void initData() {

        final RecyclerView recycler = findViewById(R.id.dialog_list_recycler);
        recycler.setLayoutManager(new CrashLinearLayoutManager(BaseApp.getContext()));
        recycler.setAdapter(adapter);

        findViewById(R.id.dialog_list_back).setOnClickListener(v -> dismiss());

        findViewById(R.id.dialog_list_next).setOnClickListener(v -> {
            if (null != listener) {
                listener.onNext();
            }
            dismiss();
        });
    }

    public void setList(List<? extends ListDialog.ListModel> list) {
        mDatas.clear();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();
    }

    private final BaseCommonAdapter<ListModel> adapter = new BaseCommonAdapter<ListModel>(mDatas, R.layout.layout_dialog_list_child) {
        @Override
        protected void onNext(RecyclerHolder holder, ListModel model, int position) {

            holder.setText(R.id.dialog_list_name, model.getName());
            holder.setText(R.id.dialog_list_time, model.getTime());
            holder.setText(R.id.dialog_list_text, model.getText());
        }
    };

    public interface ListModel {

        String getIcon();

        String getName();

        String getTime();

        String getText();
    }

    public interface OnDialogChangeListener {

        void onNext();
    }

    private OnDialogChangeListener listener;

    public void setOnDialogChangeListener(OnDialogChangeListener listener) {
        this.listener = listener;
    }
}
